package org.cttic.visualization.visualization.z_service.six;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.cttic.visualization.visualization.util.ParseJsonDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-05-27 14:39
 * 2.6.1轨迹还原准确率汇总统计
 */
@Service
public class AccuracyServiceImpl implements AccuracyService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public String accuracy(String inputData) {
        JSONObject jsonData = ParseJsonDataUtil.getJsonData(inputData);
        String provinceId = jsonData.getString("provinceId");
        String roadSectionId = jsonData.getString("roadSectionId");//路段ID
        //todo 全国查询
        if(provinceId.equals("0")){
            String querySql="SELECT sum(accRatio) as accRatio, sum(respTime) as respTime  FROM `Accuracy` ";
            System.out.println(querySql);
            Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(querySql);
            if(stringObjectMap!=null&&stringObjectMap.size()>0) {
                cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(stringObjectMap);
                Object accRatio = jsonObject.get("accRatio");
                JSONObject json = new JSONObject();
                if(accRatio!=null){
                    double accRatioD = Double.parseDouble(String.valueOf(accRatio));
                    double value = ((int)((accRatioD / 32) * 100)) / 100;
                    json.put("accRatio",value+"%");
                }
                Object respTime = jsonObject.get("respTime");
                if(respTime!=null){
                    double respTimeD = Double.parseDouble(String.valueOf(respTime));
                    System.out.println("respTimeD:"+respTimeD);
                    int value = ((int)((respTimeD / 32) * 10000)) / 100;
                    json.put("respTime","0."+value);
                }
                System.out.println("*************查询全国准确率信息："+JSONUtil.toJsonStr(json)+"**************");
                return JSONUtil.toJsonStr(json);
            }
            //todo 单省查询
        }else if(!provinceId.contains("|")){
            String querySql="SELECT accRatio ,respTime  FROM `Accuracy`  WHERE provinceId= '"+ provinceId + "'";
            System.out.println(querySql);
            Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(querySql);
            if(stringObjectMap!=null&&stringObjectMap.size()>0) {
                cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(stringObjectMap);
                Object accRatio = jsonObject.get("accRatio");
                JSONObject json = new JSONObject();
                if(accRatio!=null){
                    double accRatioD = Double.parseDouble(String.valueOf(accRatio));
                    json.put("accRatio",accRatioD+"%");
                }
                Object respTime = jsonObject.get("respTime");
                if(respTime!=null){
                    double respTimeD = Double.parseDouble(String.valueOf(respTime));
                    json.put("respTime",respTimeD);
                }
                System.out.println("*************查询"+provinceId+"准确率信息："+JSONUtil.toJsonStr(json)+"**************");
                return JSONUtil.toJsonStr(json);
            }
            //todo 多省查询
        }else if(provinceId.contains("|")){
            List<JSON> list = new ArrayList<>();
            String[] split = provinceId.split("\\|");
            for (String province : split) {
                String querySql="SELECT accRatio ,respTime  FROM `Accuracy`  WHERE provinceId= '"+ province + "'";
                System.out.println(querySql);
                Map<String, Object> stringObjectMap = jdbcTemplate.queryForMap(querySql);
                if(stringObjectMap!=null&&stringObjectMap.size()>0) {
                    cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(stringObjectMap);
                    Object accRatio = jsonObject.get("accRatio");
                    JSONObject json = new JSONObject();
                    json.put("provinceId",province);
                    if(accRatio!=null){
                        double accRatioD = Double.parseDouble(String.valueOf(accRatio));
                        json.put("accRatio",accRatioD+"%");
                    }
                    Object respTime = jsonObject.get("respTime");
                    if(respTime!=null){
                        double respTimeD = Double.parseDouble(String.valueOf(respTime));
                        json.put("respTime",respTimeD);
                    }
                    System.out.println("*************查询"+province+"准确率信息："+JSONUtil.toJsonStr(json)+"**************");
                    list.add(json);

                }
            }
            return list.toString();
        }

        return null;
    }
}
